﻿Imports System.Drawing
Imports Sgry.Azuki
Public Class TEditorSettings
    Implements System.IDisposable

    Private Const CONFIG_FILE As String = "editor.conf"
    Private _FontList As New TFontList
    Private _ShowURI As Boolean
    Private _ForeColor As Color
    Private _BackColor As Color
    Private _TabWidth As Integer
    Private _CrLfColor As Color
    Private _SpaceColor As Color
    Private _SelectedRowColor As Color
    Private _RowNumberForeColor As Color
    Private _RowNumberBackColor As Color
    Private _EofColor As Color
    Private _ShowEof As Boolean
    Private _ShowCrLf As Boolean
    Private _ShowMBSpace As Boolean
    Private _ShowSpace As Boolean
    Private _ShowTab As Boolean
    Private _ShowSelectedRow As Boolean
    Private _ShowRowNumber As Boolean
    Private _ShowRuler As Boolean
    Private _FontSize As Integer
    Private _Bold As Boolean
    Private _Italic As Boolean
    Private _FontName As String

    Public Sub New()
        Load()
    End Sub

    Public Sub ApplyAll(ByVal AControl As Control)
        For Each ctrl As Control In AControl.Controls
            If ctrl.Controls.Count > 0 Then
                ApplyAll(ctrl)
            ElseIf ctrl.GetType Is GetType(Sgry.Azuki.WinForms.AzukiControl) Then
                Apply(ctrl)
            End If
        Next
    End Sub

    Public ReadOnly Property FontCount() As Integer
        Get
            Return _FontList.Count
        End Get
    End Property
    Public ReadOnly Property Font(ByVal Index As Integer) As FontFamily
        Get
            Return _FontList.Font(Index)
        End Get
    End Property

    Public ReadOnly Property FontStyle() As FontStyle
        Get
            Dim fs As FontStyle = Drawing.FontStyle.Regular
            If Me.Bold Then
                fs = fs Or Drawing.FontStyle.Bold
            End If
            If Me.Italic Then
                fs = fs Or Drawing.FontStyle.Italic
            End If
            Return fs
        End Get
    End Property

    Public Sub Apply(ByVal AText As Sgry.Azuki.WinForms.AzukiControl)
        AText.SetKeyBind(Keys.A Or Keys.Control, AddressOf Actions.SelectAll)
        AText.SetKeyBind(Keys.C Or Keys.Control, AddressOf Actions.Copy)
        AText.SetKeyBind(Keys.X Or Keys.Control, AddressOf Actions.Cut)
        AText.SetKeyBind(Keys.V Or Keys.Control, AddressOf Actions.Paste)
        AText.SetKeyBind(Keys.Z Or Keys.Control, AddressOf Actions.Undo)
        AText.SetKeyBind(Keys.Y Or Keys.Control, AddressOf Actions.Redo)

        AText.Font = New Font(Me.FontName, Me.FontSize, Me.FontStyle)
        AText.DrawsEolCode = Me.ShowCrLf
        AText.DrawsSpace = Me.ShowSpace
        AText.DrawsFullWidthSpace = Me.ShowMBSpace
        AText.DrawsTab = Me.ShowTab
        AText.HighlightsCurrentLine = Me.ShowSelectedRow
        AText.ShowsLineNumber = Me.ShowRowNumber
        AText.ShowsHRuler = Me.ShowRuler
        AText.DrawsEofMark = Me.ShowEof
        AText.MarksUri = Me.ShowURI
        AText.ColorScheme.EolColor = Me.CrLfColor
        AText.ColorScheme.WhiteSpaceColor = Me.SpaceColor
        AText.ColorScheme.HighlightColor = Me.SelectedRowColor
        AText.ColorScheme.LineNumberFore = Me.RowNumberForeColor
        AText.ColorScheme.LineNumberBack = Me.RowNumberBackColor
        AText.ColorScheme.EofColor = Me.EofColor

        AText.TabWidth = Me.TabWidth
        AText.ForeColor = Me.ForeColor
        AText.BackColor = Me.BackColor
    End Sub
    Public Property ShowURI() As Boolean
        Get
            Return _ShowURI
        End Get
        Set(ByVal value As Boolean)
            _ShowURI = value
        End Set
    End Property
    Public Property ForeColor() As Color
        Get
            Return _ForeColor
        End Get
        Set(ByVal value As Color)
            _ForeColor = value
        End Set
    End Property
    Public Property BackColor() As Color
        Get
            Return _BackColor
        End Get
        Set(ByVal value As Color)
            _BackColor = value
        End Set
    End Property
    Public Property Bold() As Boolean
        Get
            Return _Bold
        End Get
        Set(ByVal value As Boolean)
            _Bold = value
        End Set
    End Property
    Public Property Italic() As Boolean
        Get
            Return _Italic
        End Get
        Set(ByVal value As Boolean)
            _Italic = value
        End Set
    End Property
    Public Property FontSize() As Integer
        Get
            Return _FontSize
        End Get
        Set(ByVal value As Integer)
            _FontSize = value
        End Set
    End Property

    Public Property FontName() As String
        Get
            Return _FontName
        End Get
        Set(ByVal value As String)
            _FontName = value
        End Set
    End Property

    Public Sub Load()
        Dim IniFile As New TIniFile(Me.ConfigFile)
        Try
            Me.FontName = IniFile.ReadString("Editor", "FontName", "MS UI Gothic")
            Me.FontSize = IniFile.ReadInteger("Editor", "FontSize", 9)
            Me.Bold = IniFile.ReadBool("Editor", "Bold", False)
            Me.Italic = IniFile.ReadBool("Editor", "Italic", False)
            Me.ShowCrLf = IniFile.ReadBool("CrLf", "Show", True)
            Me.ShowSpace = IniFile.ReadBool("Space", "Show", True)
            Me.ShowMBSpace = IniFile.ReadBool("MBSpace", "Show", True)
            Me.ShowTab = IniFile.ReadBool("Tab", "Show", True)
            Me.ShowSelectedRow = IniFile.ReadBool("SelectedRow", "Show", True)
            Me.ShowRowNumber = IniFile.ReadBool("RowNumber", "Show", True)
            Me.ShowRuler = IniFile.ReadBool("Ruler", "Show", True)
            Me.ShowEof = IniFile.ReadBool("Eof", "Show", True)
            Me.ShowURI = IniFile.ReadBool("URI", "Show", True)
            Me.CrLfColor = IniFile.ReadColor("CrLf", "Color", Color.Red)
            Me.SpaceColor = IniFile.ReadColor("Space", "Color", Color.White)
            Me.SelectedRowColor = IniFile.ReadColor("SelectedRow", "Color", Color.Red)
            Me.RowNumberForeColor = IniFile.ReadColor("RowNumber", "ForeColor", Color.Yellow)
            Me.RowNumberBackColor = IniFile.ReadColor("RowNumber", "BackColor", Color.White)
            Me.EofColor = IniFile.ReadColor("Eof", "Color", Color.Red)

            Me.TabWidth = IniFile.ReadInteger("Tab", "Width", 8)
            Me.ForeColor = IniFile.ReadColor("Editor", "ForeColor", Color.Black)
            Me.BackColor = IniFile.ReadColor("Editor", "BackColor", Color.White)
        Finally
            IniFile.Dispose()
        End Try
    End Sub

    Public Sub Save()
        Dim IniFile As New TIniFile(Me.ConfigFile)
        Try
            IniFile.WriteString("Editor", "FontName", _FontName)
            IniFile.WriteInteger("Editor", "FontSize", _FontSize)
            IniFile.WriteBool("Editor", "Bold", _Bold)
            IniFile.WriteBool("Editor", "Italic", _Italic)
            IniFile.WriteBool("CrLf", "Show", _ShowCrLf)
            IniFile.WriteBool("Space", "Show", _ShowSpace)
            IniFile.WriteBool("MBSpace", "Show", _ShowMBSpace)
            IniFile.WriteBool("Tab", "Show", _ShowTab)
            IniFile.WriteBool("SelectedRow", "Show", _ShowSelectedRow)
            IniFile.WriteBool("RowNumber", "Show", _ShowRowNumber)
            IniFile.WriteBool("Ruler", "Show", _ShowRuler)
            IniFile.WriteBool("Eof", "Show", _ShowEof)
            IniFile.WriteColor("CrLf", "Color", _CrLfColor)
            IniFile.WriteColor("Space", "Color", _SpaceColor)
            IniFile.WriteColor("SelectedRow", "Color", _SelectedRowColor)
            IniFile.WriteColor("RowNumber", "ForeColor", _RowNumberForeColor)
            IniFile.WriteColor("RowNumber", "BackColor", _RowNumberBackColor)
            IniFile.WriteColor("Eof", "Color", _EofColor)
            IniFile.WriteBool("URI", "Show", _ShowURI)
            IniFile.WriteInteger("Tab", "Width", _TabWidth)
            IniFile.WriteColor("Editor", "ForeColor", _ForeColor)
            IniFile.WriteColor("Editor", "BackColor", _BackColor)
        Finally
            IniFile.Dispose()
        End Try
    End Sub
    Public Property TabWidth() As Integer
        Get
            Return _TabWidth
        End Get
        Set(ByVal value As Integer)
            _TabWidth = value
        End Set
    End Property
    Public Property CrLfColor() As Color
        Get
            Return _CrLfColor
        End Get
        Set(ByVal value As Color)
            _CrLfColor = value
        End Set
    End Property
    Public Property SpaceColor() As Color
        Get
            Return _SpaceColor
        End Get
        Set(ByVal value As Color)
            _SpaceColor = value
        End Set
    End Property
    Public Property SelectedRowColor() As Color
        Get
            Return _SelectedRowColor
        End Get
        Set(ByVal value As Color)
            _SelectedRowColor = value
        End Set
    End Property
    Public Property RowNumberForeColor() As Color
        Get
            Return _RowNumberForeColor
        End Get
        Set(ByVal value As Color)
            _RowNumberForeColor = value
        End Set
    End Property
    Public Property RowNumberBackColor() As Color
        Get
            Return _RowNumberBackColor
        End Get
        Set(ByVal value As Color)
            _RowNumberBackColor = value
        End Set
    End Property
    Public Property EofColor() As Color
        Get
            Return _EofColor
        End Get
        Set(ByVal value As Color)
            _EofColor = value
        End Set
    End Property
    Public Property ShowEof() As Boolean
        Get
            Return _ShowEof
        End Get
        Set(ByVal value As Boolean)
            _ShowEof = value
        End Set
    End Property
    Public Property ShowCrLf() As Boolean
        Get
            Return _ShowCrLf
        End Get
        Set(ByVal value As Boolean)
            _ShowCrLf = value
        End Set
    End Property
    Public Property ShowMBSpace() As Boolean
        Get
            Return _ShowMBSpace
        End Get
        Set(ByVal value As Boolean)
            _ShowMBSpace = value
        End Set
    End Property
    Public Property ShowSpace() As Boolean
        Get
            Return _ShowSpace
        End Get
        Set(ByVal value As Boolean)
            _ShowSpace = value
        End Set
    End Property
    Public Property ShowTab() As Boolean
        Get
            Return _ShowTab
        End Get
        Set(ByVal value As Boolean)
            _ShowTab = value
        End Set
    End Property
    Public Property ShowSelectedRow() As Boolean
        Get
            Return _ShowSelectedRow
        End Get
        Set(ByVal value As Boolean)
            _ShowSelectedRow = value
        End Set
    End Property
    Public Property ShowRowNumber() As Boolean
        Get
            Return _ShowRowNumber
        End Get
        Set(ByVal value As Boolean)
            _ShowRowNumber = value
        End Set
    End Property
    Public Property ShowRuler() As Boolean
        Get
            Return _ShowRuler
        End Get
        Set(ByVal value As Boolean)
            _ShowRuler = value
        End Set
    End Property

    Private ReadOnly Property ConfigFile() As String
        Get
            Return IncludeTrailingPathDelimiter(System.AppDomain.CurrentDomain.BaseDirectory) & CONFIG_FILE
        End Get
    End Property

    Private Function IncludeTrailingPathDelimiter(ByVal APath As String) As String
        If Left(APath, 1) = "\" Then
            Return APath
        Else
            Return APath & "\"
        End If
    End Function

    Private disposedValue As Boolean = False        ' 重複する呼び出しを検出するには

    ' IDisposable
    Protected Overridable Sub Dispose(ByVal disposing As Boolean)
        If Not Me.disposedValue Then
            If disposing Then
                ' TODO: 他の状態を解放します (マネージ オブジェクト)。
            End If
            _FontList.Dispose()
            ' TODO: ユーザー独自の状態を解放します (アンマネージ オブジェクト)。
            ' TODO: 大きなフィールドを null に設定します。
        End If
        Me.disposedValue = True
    End Sub

#Region " IDisposable Support "
    ' このコードは、破棄可能なパターンを正しく実装できるように Visual Basic によって追加されました。
    Public Sub Dispose() Implements IDisposable.Dispose
        ' このコードを変更しないでください。クリーンアップ コードを上の Dispose(ByVal disposing As Boolean) に記述します。
        Dispose(True)
        GC.SuppressFinalize(Me)
    End Sub
#End Region

End Class
